//
//  HPToDoMainViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 12/30/15.
//  Copyright © 2015 zhuangyihang. All rights reserved.
//

#import "HPToDoMainViewController.h"

#import "ToDoMainItemTableViewCell.h"
#import "HPAddViewController.h"
#import <JTCalendar/JTCalendar.h>

static const NSInteger kCalendarMenuHeight = 40;
static const NSInteger kCalendarContentHeight = 300;

@interface HPToDoMainViewController ()<UITableViewDataSource,UITableViewDelegate,JTCalendarDelegate>

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) NSDate *selectDate;
@property (strong, nonatomic) JTCalendarManager *calendarManager;

@end

@implementation HPToDoMainViewController

- (id)init{
    self = [super init];
    if (self) {
        self.tabBarItem.title = @"我";
        self.tabBarItem.image = [UIImage imageNamed:@"Tab_Second"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"Tab_SecondHL"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.datas = @[@"",@"",@""];
    
    [self setBaseNavigationBarTitle:@"待办事宜"];
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return [self tableView:tableView cellForCalendarRowAtIndexPath:indexPath];
    }else{
        return [self tableView:tableView cellForItemRowAtIndexPath:indexPath];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForCalendarRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"ToDoMainCalendarCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
                                        reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _calendarManager = [JTCalendarManager new];
        _calendarManager.delegate = self;
        
        
        JTCalendarMenuView *menu = [JTCalendarMenuView new];
        [cell.contentView addSubview:menu];
        [menu mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.top.centerX.equalTo(cell.contentView);
            make.height.mas_equalTo(kCalendarMenuHeight);
        }];
        [_calendarManager setMenuView:menu];
        
        JTHorizontalCalendarView *content = [JTHorizontalCalendarView new];
        [cell.contentView addSubview:content];
        [content mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.centerX.equalTo(cell.contentView);
            make.top.equalTo(menu.mas_bottom);
            make.height.mas_equalTo(kCalendarContentHeight);
        }];
        
        [_calendarManager setContentView:content];
        [_calendarManager setDate:[NSDate date]];
        
    }
    
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForItemRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"ToDoMainItemCell";
    ToDoMainItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ToDoMainItemTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
                                        reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return kCalendarMenuHeight + kCalendarContentHeight;
    }else{
        return [ToDoMainItemTableViewCell getCellHeight];
    }
    
    return 0;
}

#pragma mark -JTCalendarDelegate
- (void)calendar:(JTCalendarManager *)calendar prepareDayView:(JTCalendarDayView *)dayView
{
    dayView.hidden = NO;
    
    // Other month
    if([dayView isFromAnotherMonth]){
        dayView.hidden = YES;
    }
    // Today
    else if([_calendarManager.dateHelper date:[NSDate date] isTheSameDayThan:dayView.date]){
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = [UIColor blueColor];
        dayView.dotView.backgroundColor = [UIColor whiteColor];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }
    // Selected date
    else if(self.selectDate && [_calendarManager.dateHelper date:self.selectDate isTheSameDayThan:dayView.date]){
        dayView.circleView.hidden = NO;
        dayView.circleView.backgroundColor = [UIColor redColor];
        dayView.dotView.backgroundColor = [UIColor whiteColor];
        dayView.textLabel.textColor = [UIColor whiteColor];
    }
    // Another day of the current month
    else{
        dayView.circleView.hidden = YES;
        dayView.dotView.backgroundColor = [UIColor redColor];
        dayView.textLabel.textColor = [UIColor blackColor];
    }
    
//    if([self haveEventForDay:dayView.date]){
//        dayView.dotView.hidden = NO;
//    }
//    else{
//        dayView.dotView.hidden = YES;
//    }
}

- (void)calendar:(JTCalendarManager *)calendar didTouchDayView:(JTCalendarDayView *)dayView
{
    self.selectDate = dayView.date;
    
    // Animation for the circleView
    dayView.circleView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    [UIView transitionWithView:dayView
                      duration:.3
                       options:0
                    animations:^{
                        dayView.circleView.transform = CGAffineTransformIdentity;
                        [_calendarManager reload];
                    } completion:nil];
    
    
    // Load the previous or next page if touch a day from another month
//    
//    if(![_calendarManager.dateHelper date:_calendarContentView.date isTheSameMonthThan:dayView.date]){
//        if([_calendarContentView.date compare:dayView.date] == NSOrderedAscending){
//            [_calendarContentView loadNextPageWithAnimation];
//        }
//        else{
//            [_calendarContentView loadPreviousPageWithAnimation];
//        }
//    }
}

#pragma mark -
#pragma mark Private
- (void)setupUI{
    
    [self setRightButtonImage:[UIImage imageNamed:@"FirstTab_AddToDo"]];
    [self setLeftButtonImage:[UIImage imageNamed:@"FirstTab_BackToToday"]];
    
    [self addRightButtonSelector:@selector(add)];
    
    UITableView *tableView = [UITableView new];
    [self.baseContentView addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.baseContentView);
        make.center.equalTo(self.baseContentView);
    }];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.tableFooterView = [UIView new];
    
}

#pragma mark - Click
- (void)add{
    HPAddViewController *v = [[HPAddViewController alloc] init];
    v.isAdd = YES;
    [self.navigationController pushViewController:v animated:YES];
}
@end
